<template>
  <div class="BoxGrouping">
    <div>
      <div>
        <p>关联组名称</p>
        <input v-model="from2.name" type="text" />
      </div>
      <el-button @click="search" type="primary">搜索</el-button>
      <el-button @click="inserts" type="success">添加</el-button>
    </div>
    <el-table :data="tableData.list" border style="width: 100%;">
      <el-table-column prop="id" label="分组ID" width="180"></el-table-column>
      <el-table-column prop="name" label="关联组名称" width="180"></el-table-column>
      <el-table-column prop="x" label="X值" width="180"></el-table-column>
      <el-table-column prop="y" label="Y值" width="180"></el-table-column>
      <el-table-column prop="updatedAt" label="最后操作时间" width="180"></el-table-column>
      <el-table-column prop="updatedAdminId" label="操作人"></el-table-column>
      <el-table-column fixed="right" label="操作" width="120">
        <template slot-scope="scope">
          <div v-if="scope.row.state == 1">
            <el-button @click="handleClick(scope)" type="text" size="small">编辑</el-button>
            <el-button @click="detil(scope)" type="text" size="small">停用</el-button>
          </div>
          <div v-else>
                <el-button style="color:red;" type="text"  size="small" >已停用</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="currentChange"
      background
      layout="prev, pager, next"
      :total="tableData.pages*10"
    ></el-pagination>
    <div class="Modification" v-if="show">
      <div>
        <p @click="handleClickto">x</p>
        <div>
          <span>分组名称</span>
          <input v-model="from.name" type="text" />
        </div>
        <div>
          <span>Y值</span>
          <input v-model="from.y" type="text" />
        </div>
        <div>
          <span>Y自增率</span>
          <input v-model="from.yIncrease" type="text" />
        </div>
        <el-button @click="insert" v-if="bool==1" type="success">增加</el-button>
        <el-button @click="updata" v-else type="success">修改</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import axios from "../../../utils/publicApi";
import { Loading } from 'element-ui';
export default {
  data() {
    return {
      show: false,
      bool: 1,
      tableData: "",
      from: {
        name: "",
        y: "",
        yIncrease: ""
      },
      from1: {
        name: "",
        currentPage: 1,
        pageSize: 5
      },
      from2: {
        name: "",
        currentPage: 1,
        pageSize: 5
      }
    };
  },
  created() {
    axios
      .getAll("/gamemall/boxGroup/list", { currentPage: 1, pageSize: 5 })
      .then(res => {
        if (res.code == 200) {
          this.tableData = res.obj;
        }
      });
  },
  methods: {
    inserts() {
      this.from.name = "";
      this.from.y = "";
      this.from.yIncrease = "";
      this.bool = 1;
      this.show = true;
    },
    detil(e) {
      e.row.state = 0;
      axios.postAll("/gamemall/boxGroup/update", e.row).then(res => {
        if(res.code == 200){
          this.$message({
              showClose: true,
              message: '已停用',
              type: 'success'
              });
        }else{
          this.$message({
              showClose: true,
              message: '停用失败',
              type: 'error'
              });
        }
      });
    },
    updata() {
      axios.postAll("/gamemall//boxGroup/update", this.from).then(res => {
        if (res.code == 200) {
          this.$message({
              showClose: true,
              message: '更新成功',
              type: 'success'
              });
          this.show = false;
          
          axios
            .getAll("/gamemall/boxGroup/list", {
              currentPage: this.from1.currentPage,
              pageSize: 5
            })
            .then(res => {
              if (res.code == 200) {
                this.tableData = res.obj;
              }
            });
        }else{
          this.$message({
              showClose: true,
              message: '更新失败',
              type: 'error'
              });
        }
      });
    },
    search() {
       var options = false
      Loading.service(options);
      this.from1 = JSON.parse(JSON.stringify(this.from2));
      axios.getAll("/gamemall/boxGroup/list", this.from2).then(res => {
        if (res.code == 200) {
          this.tableData = res.obj;
          let loadingInstance = Loading.service(options);
          this.$nextTick(() => { 
            loadingInstance.close();
          });
        }
      });
    },
    handleClick(e) {
      this.show = true;
      this.from.name = e.row.name;
      this.from.y = e.row.y;
      this.from.yIncrease = e.row.yIncrease;
      this.from.id = e.row.id;
      this.bool = 0;
    },
    handleClickto() {
      this.show = false;
    },
    insert() {
      if (this.from.yIncrease.length != 0) {
        this.from.yIncrease = this.from.yIncrease * 1;
      }
      axios.postAll("/gamemall/boxGroup/insert", this.from).then(res => {
        if (res.code == 200) {
          this.$message({
              showClose: true,
              message: '新增成功',
              type: 'success'
              });
          this.show = false;
          axios
            .getAll("/gamemall/boxGroup/list", {
              currentPage: this.from1.currentPage,
              pageSize: 5
            })
            .then(res => {
              if (res.code == 200) {
                this.tableData = res.obj;
              }
            });
        }else{
          this.$message({
              showClose: true,
              message: '新增失败',
              type: 'error'
              });
        }
      });
    },
    currentChange(e) {
      var options = false
      Loading.service(options);
      this.from2.currentPage = e;
      this.from1.currentPage = e;
      axios.getAll("/gamemall/boxGroup/list", this.from1).then(res => {
        if (res.code == 200) {
          this.tableData = res.obj;
          let loadingInstance = Loading.service(options);
          this.$nextTick(() => { 
            loadingInstance.close();
          });
        }
      });
    }
  }
};
</script>
<style lang="less" >
@import "../../../style/mixin.less";
.BoxGrouping {
  .el-pagination {
    text-align: center;
  }
  text-align: left;
  input {
    border: 1px solid #ccc;
    height: 30px;
  }
  > div:nth-of-type(1) {
    margin: 50px 0;
    .el-button {
      margin: 0 20px;
    }
    div {
      display: inline-block;
      margin-right: 100px;
    }
    p {
      display: inline-block;
      margin-right: 50px;
    }
  }
  .Modification {
    margin: 0;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 999;
    input {
      .w(300);
    }
    > div {
      .el-button {
        position: absolute;
        left: 40%;
      }
      > p {
        color: black;
        position: absolute;
        right: 20px;
        top: 5px;
        cursor: pointer;
      }
      background: rgba(255, 255, 255, 0.9);
      display: inline-block;
      position: absolute;
      top: 30%;
      left: 30%;
      padding-bottom: 70px;
      > div {
        text-align: left;
        margin: 50px;
        span {
          display: inline-block;
          .w(100);
        }
      }
    }
  }
}
</style>